﻿

function trim(s) { return (s.replace(/^(\s*|　*)/, "")).replace(/(\s*|　*)$/, ""); }

var register = {
  lampid: null,
  lampname: null,
  prodate: null,
  adate: null,
  lpole: null,
  lkzq: null,
  RPower: null,
  APower: null,
  comid: null,
  init: function() {
    var lamp = document.getElementById("txtLampID");
    if (!lamp.disabled) {
      lamp.addEventListener("blur", reg_chkdevid);
      this.lampid = false;
      this.lampname = false;
      this.prodate = false;
      this.adate = false;
      this.lpole = false;
      this.lkzq = false;
      this.RPower = false;
      this.APower = false;
      this.comid = false;
    } else {
      this.lampid = true;
      this.lampname = true;
      this.prodate = true;
      this.adate = true;
      this.lpole = true;
      this.lkzq = true;
      this.RPower = true;
      this.APower = true;
      this.comid = true;
    }
    lamp = document.getElementById("txtLampName");
    lamp.addEventListener("blur", reg_chkdevname);
    lamp = document.getElementById("txtProduceDate");
    lamp.addEventListener("blur", reg_chkprodate);
    lamp.addEventListener("focus", reg_focusprodate);
    lamp = document.getElementById("txtR_Power");
    lamp.addEventListener("blur", reg_chk_RPower);
    lamp = document.getElementById("txtA_Power");
    lamp.addEventListener("blur", reg_chk_APower);
    lamp = document.getElementById("txtCommunicationID");
    lamp.addEventListener("blur", reg_chk_ComID);
  },
  //检查光源编码   2013-1-15   guoh
  chk_id: function() {
    var lid = $("#txtLampID");
    var str = lid.val();

    if (trim(str) == "") {
      lid.next("span").addClass("error").html("<span class='ico__error'></span>" + "光源编号为空");
      lid.addClass("form_input_error");
      register.lampid = false;
      return false;
    }
    if (str.length != 6) { //长度不正确
      lid.next("span").addClass("error").html("<span class='ico__error'></span>" + "编码长度不正确");
      lid.addClass("form_input_error");
      register.lampid = false;
      return false;
    }

    str1 = str.substring(0, 1);
    str2 = str.substring(1, 6);
    if ((str1 >= "a" && str1 <= "z") || (str1 >= "A" && str1 <= "Z") && str2.match(/\D/) == null) {
      var rt = ajaxmethodHtml("Services/GY.ashx?method=CheckPrimaryKey", str);
      if (rt == "0") {
        lid.next("span").addClass("pass").html("<span class='ico__pass'></span>");
        lid.removeClass("form_input_error");
        register.lampid = true;
        return true;
      } else {
        lid.next("span").addClass("error").html("<span class='ico__error'></span>" + "光源编号已存在");
        lid.addClass("form_input_error");
        register.lampid = false;
        return false;
      }
    } else {
      lid.next("span").addClass("error").html("<span class='ico__error'></span>" + "编码格式不正确（示例：J00001）");
      register.lampid = false;
      return false;
    }
  },
  //检查光源名称   2013-1-18   guoh
  chk_lampname: function() {
    var lname = $("#txtLampName");
    var str = lname.val();

    if (trim(str) == "") {
      lname.next("span").addClass("error").html("<span class='ico__error'></span>" + "光源名称为空");
      lname.addClass("form_input_error");
      register.lampname = false;
      return false;
    } else {
      lname.next("span").addClass("pass").html("<span class='ico__pass'></span>");
      lname.removeClass("form_input_error");
      register.lampname = true;
      return true;
    }
  },
  //检查额定功率   2013-2-18   guoh
  chk_RPower: function() {
    var rpower = $("#txtR_Power");
    var str = rpower.val();

    if (isNaN(str)) {
      rpower.next("span").addClass("error").html("<span class='ico__error'></span>" + "额定功率必须是数字");
      rpower.addClass("form_input_error");
      register.RPower = false;
      return false;
    } else {
      rpower.next("span").addClass("pass").html("<span class='ico__pass'></span>");
      rpower.removeClass("form_input_error");
      register.RPower = true;
      return true;
    }
  },
  //检查实测功率   2013-2-18   guoh
  chk_APower: function() {
    var apower = $("#txtA_Power");
    var str = apower.val();

    if (isNaN(str)) {
      apower.next("span").addClass("error").html("<span class='ico__error'></span>" + "实测功率必须是数字");
      apower.addClass("form_input_error");
      register.APower = false;
      return false;
    } else {
      apower.next("span").addClass("pass").html("<span class='ico__pass'></span>");
      apower.removeClass("form_input_error");
      register.APower = true;
      return true;
    }
  },
  //检查通道ID   2013-2-18   guoh
  chk_ComID: function() {
    var temp = $("#txtCommunicationID");
    var str = temp.val(); //pauseInt(temp.val());
    var reg1 = /^\d+$/;

    if (str.match(reg1) == null) {
      temp.next("span").addClass("error").html("<span class='ico__error'></span>" + "通道ID必须是整数");
      temp.addClass("form_input_error");
      register.comid = false;
      return false;
    } else {
      temp.next("span").addClass("pass").html("<span class='ico__pass'></span>");
      temp.removeClass("form_input_error");
      register.comid = true;
      return true;
    }
  },
  //检查出厂日期
  chk_prodate: function() {
    var prodt = $("#txtProduceDate");
    var str = prodt.val();

    if (trim(str) == "") {
      prodt.next("span").addClass("error").html("<span class='ico__error'></span>" + "出厂日期为空");
      prodt.addClass("form_input_error");
      register.prodate = false;
      return false;
    } else {
      prodt.next("span").addClass("pass").html("<span class='ico__pass'></span>");
      prodt.removeClass("form_input_error");
      register.prodate = true;
      return true;
    }
  },
  //添加控制器信息   2013-1-24   guoh
  register_save: function() {
    if (this.lampid == true &&
        this.lampname == true &&
        this.prodate == true &&
        this.RPower == true &&
        this.APower == true &&
        this.comid == true
    ) {
      return true;
    } else {
      if (this.lampid == null) {
        register.chk_id();
      }
      if (this.lampname == null) {
        register.chk_lampname();
      }
      if (this.prodate == null) {
        register.chk_prodate();
      }
      if (this.RPower == null) {
        register.chk_RPower();
      }
      if (this.APower == null) {
        register.chk_APower();
      }
      if (this.comid == null) {
        register.chk_ComID();
      }
      return false;
    }
  },

};

//guoh
function reg_save() {
  return register.register_save();
}

function reg_chkdevid() {
  register.chk_id();

}

function reg_chkdevname() {
  register.chk_lampname();
}

function reg_chkprodate() {
  register.chk_prodate();
}

function reg_chk_RPower() {
  register.chk_RPower();
}

function reg_chk_APower() {
  register.chk_APower();
}

function reg_chk_ComID() {
  register.chk_ComID();
}

function reg_focusprodate() {
  register.chk_prodate();
}

function register_init() {
  register.init();
}

function fee_register_init() {
  register.init_fee();
}

function other_register_init() {
  register.init_other();
}

function register_step2_init() {
  register.init_2();
  document.onkeyup = function(event) {
    event = event || window.event;
    if (event.keyCode == 13) {
      register.S2_save();
    }
  };
}

function other_popup_init() {
  register.init_other_popup();
}

function password_init(params) {
  ma_pswd.init(params);
}

function set_password_init() {
  Setpwd.init();
}